package breeze.pixel.weather.apps_util.utils.appsettings.settingsview;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;

import breeze.pixel.weather.BaseView;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;
import breeze.pixel.weather.service.app_widget.WeatherUtils;

public class ChangeAppwidgetAlphaView extends BaseView {

    private Button save;
    private ImageView bg;
    private MTextView progress;
    private SeekBar seek;
    int alphas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_alpha);
        getViews();
        setData();
    }

    private void getViews() {
        ImageView weather_alpha = findViewById(R.id.alpha_weather_icon);
        weather_alpha.setImageBitmap(AppToolUtil.getweatherIcon(this, "100"));
        save = findViewById(R.id.alpha_save);
        bg = findViewById(R.id.alpha_bg);
        progress = findViewById(R.id.alpha_progress);
        seek = findViewById(R.id.alpha_seek);
    }

    private void setData() {
        seek.setMax(255);
        int alpha = sets.getAppWidgetAlpha();
        if (alpha <= 0) {
            alpha = 0;
        }
        seek.setProgress(alpha);
        progress.setText(String.format("%s/255", alpha));
        bg.setAlpha(alpha);
        seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar p1, int p2, boolean p3) {
                int alpha_bg = 0;
                if (p2 > 0) {
                    alpha_bg = Math.min(p2, 255);
                }
                bg.setAlpha(alpha_bg);
                progress.setText(String.format("%s/255", alpha_bg));
                alphas = p2;
            }

            @Override
            public void onStartTrackingTouch(SeekBar p1) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar p1) {
            }

        });
        save.setOnClickListener(p1 -> {
            sets.setAppWidgetAlpha(alphas);
            toast("保存成功");
            WeatherUtils.updateView(null);
            finish();
        });
    }

    @Override
    public void init() {

    }

    @Override
    public void initData() {

    }

    public void saveAlpha(View view) {

    }
}